//
//  CHBMainTabBarController.swift
//  weibo.chb
//
//  Created by 褚红彪 on 16/1/19.
//  Copyright © 2016年 chuhongbiao. All rights reserved.
//
import UIKit
class CHBMainTabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let tabBar = CHBTabBar()
        //self.tabBar = tabBar
        //系统的属性如果是只读属性那么用KVC给这个属性赋值
        setValue(tabBar, forKeyPath: "tabBar")
        //这个时候出现了循环引用的情况，解决办法就是将self用弱指针weak修饰
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        tabBar.composeButtonClosure = { [weak self] in
            print("撰写按钮点击啦")
            print(self!.view)
        }
        addChildViewController(CHBHomeTableViewController(), imgName: "tabbar_home", title: "首页")
        addChildViewController(CHBDiscoverTableViewController(), imgName: "tabbar_discover", title: "发现")
        addChildViewController(CHBMessageTableViewController(), imgName: "tabbar_message_center", title: "消息")
        addChildViewController(CHBProfileTableViewController(), imgName: "tabbar_profile", title: "个人中心")
        //self.performSelector("dismiss", withObject: nil, afterDelay: 3.0)
        // Do any additional setup after loading the view.
    }
    //方法不能写成私有的否则会报错
    /*func dismiss(){
        dismissViewControllerAnimated(false, completion: nil)
    }*/
    func addChildViewController(childController: UIViewController, imgName: String, title: String) {
        //设置tabBar的文字
        //childController.tabBarItem.title = title
        //设置导航栏的标题
        //childController.navigationItem.title = title
        //可以将tabBar和导航栏的文字一起设置
        childController.title = title
        //设置图片
        childController.tabBarItem.image = UIImage(named: imgName)
        //将tabBar的图片渲染方式改成原生的（默认是蓝色）
        childController.tabBarItem.selectedImage = UIImage(named: "\(imgName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //将tabBar的文字颜色改成橘黄色（默认是蓝色的）
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Selected)
        //添加子控制器（用导航控制器包一下）
        addChildViewController(CHBNavigationViewController(rootViewController: childController))
        //addChildViewController(childController)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    /*deinit{
       print("控制器销毁")
    }*/
}
